home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15714 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  98 lines

  1. Newsgroups: comp.lang.c
  2. Path: netcom.com!smryan
  3. From: smryan@netcom.com (@#$%!?!)
  4. Subject: Re: [Q] Best handling of non-fixed-length data??
  5. Message-ID: <smryanDq6Jys.GnD@netcom.com>
  6. Organization: The Programmer formerly known as S M Ryan
  7. X-Newsreader: TIN [version 1.2 PL1]
  8. References: <4l629f$4cc@nuke.csu.net>
  9. Date: Sat, 20 Apr 1996 21:35:16 GMT
  10. Sender: smryan@netcom4.netcom.com
  11.  
  12. : Hi there, and thanks in advance for taking the time.  
  13. : I consider myself an adept, however novice, programmer in C;
  14. : but I need advice on the best way to manage non-fixed-length data.
  15.  
  16. Externally, you can use various organisations like
  17.     <question number><answer text>\n
  18.     ...
  19. with repeatable question numbers or
  20.     <question number> (<answer text>;....)\n
  21. with appropriate delimiters or
  22.     <question number> <number of answers> <answer length1><answer1> ...
  23.  
  24. Internally, you can have an array <questionNumber,answerText> with 
  25. repeatable questionNumbers in the array, or <questionNumber,array of
  26. answerTexts>.
  27.  
  28.  
  29. If you have all the lengths precomputed and read in with the data, you
  30. can allocate all of the various arrays and strings as you see them and
  31. then just fill them in.
  32.  
  33. If you only know the lengths once you've read the entire value, you can
  34. read once for length, backup, and reread for content. Or you can adjust
  35. the sizes of arrays as you go along, or create a linked list.
  36.  
  37. Letting T be some type, then you can add one more element to an array
  38. of T with something like:
  39.  
  40.     T *arr=0; int arrlen=0,arrx=0;
  41.  
  42.     T *addOneMoreT(T *array,int *length,int *actual,T value) {
  43.         /*    Initialise.                    */
  44.         if (*atual==0) {*actual = 1; array = malloc(sizeof(T));}
  45.  
  46.         /*    Ensure array is long enough for another.    */
  47.         if (*length>=*actual) {
  48.             *actual += *actual;
  49.             array = realloc(array,*actual * sizeof(T));
  50.         }
  51.  
  52.         /*    Add the element.                */
  53.         array[*length] = value;
  54.         *length += 1;
  55.     }
  56.  
  57. Keep calling arr=addOneMoreT(arr,&arrlen,&arrx,val) for each question
  58. answer pair, or each answer of an array, or each character of a string.
  59. Once you've established the maximum size, you can clean up with
  60.     arr = realloc(arr,arrlen*sizeof(T));
  61. And then you iterate through the array with
  62.     int i;
  63.     for (i=0; i<arrlen; i++)
  64.         ...arr[i]...
  65.  
  66. or    T *elem;
  67.     for (elem=arr; elem<arr+arrlen; elem++)
  68.         ...*elem...
  69.  
  70.  
  71. You can also use linked lists
  72.  
  73.     typedef struct T tT,*pT;
  74.     struct T {
  75.         pT    link;
  76.         D    data;
  77.     };
  78.  
  79.     pT addOneMoreT(pT link,D value) {
  80.         pT result = malloc(sizeof(tT));
  81.         result->link = link;
  82.         result->data = value;
  83.         return result;
  84.     }
  85.  
  86.     pT arr=0;
  87.  
  88. Keeping calling arr=addOneMoreT(arr,value) for element. And then you iterate
  89. through the array with
  90.     pT elem;
  91.     for (elem=arr; elem; elem=elem->link)
  92.         ...elem->data...
  93. -- 
  94. The Queen who loves, the Queen of life,    | smryan@netcom.com  PO Box 1563
  95. the Queen who straits, the Queen of strife;|          Cupertino, California
  96. with gasp of death or gift of breath       | (xxx)xxx-xxxx            95015
  97. she brings the choice of birth or knife.   |         I don't use no smileys
  98.